﻿using System;
using Myotragus.Data.Containers;

namespace Myotragus.Data.Repositories
{
	public class Repository<TEntity> : IRepository<TEntity>
	{
		protected IEntitiesContainer Entities
		{
			get;
			private set;
		}

		protected readonly Type TypeOfTEntity = typeof(TEntity);

		public Repository(IEntitiesContainer entities)
		{
			if (entities == null)
				throw new ArgumentNullException("entities");

			Entities = entities;
			EntityImplementation = Entities.ResolveType(TypeOfTEntity);
		}

		public Type EntityImplementation { get; private set; }

		public TEntity New()
		{
			return (TEntity)Entities.Create(TypeOfTEntity);
		}

		public TEntity New(object prototype)
		{
			if (prototype == null)
				throw new ArgumentNullException("prototype");

			return (TEntity)Entities.Create(TypeOfTEntity, prototype);
		}
	}
}